Skip to content

Conversation

@rodrigoprimo
Copy link
Collaborator

@rodrigoprimo rodrigoprimo commented Sep 18, 2025

Description

In preparation for PHPCS 4.0, which changes the tokenization of namespaced names, this PR adds tests with all variants of namespace names (unqualified, partially qualified, fully qualified, and namespace relative) to the WordPress.Security.EscapeOutput sniff. This is a continuation of #2581, where tests were added for all sniffs extending AbstractFunctionRestrictions except EscapeOutput.

This PR also includes tests to ensure the basename( __FILE__ ) pattern recognition in _deprecated_file() only applies to global basename() function calls, not to other constructs that might look similar (such as namespaced variants or class methods).

@rodrigoprimo
Copy link
Collaborator Author

rodrigoprimo commented Sep 18, 2025

I'm investigating the test that is failing in the PHPCS lowest build.

Fixed and force-pushed. The problem was related to a change in how PHPCS tokenizes FQN exit/die. I added more details in a code comment.

@rodrigoprimo
Copy link
Collaborator Author

Moved to draft pending the review of #2620

@rodrigoprimo rodrigoprimo force-pushed the escape-output-namespaced-names-tests branch from 490c471 to 0d6763d Compare November 14, 2025 14:32
…attern

Add tests to ensure the `basename( __FILE__ )` pattern recognition in `_deprecated_file()` only applies to global `basename()` function calls, not to other constructs that might look similar.
@rodrigoprimo rodrigoprimo force-pushed the escape-output-namespaced-names-tests branch from 0d6763d to 322829b Compare November 14, 2025 14:43
@rodrigoprimo rodrigoprimo marked this pull request as ready for review November 24, 2025 16:04
@rodrigoprimo
Copy link
Collaborator Author

@jrfnl, I'm moving this PR back to ready for review.

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodrigoprimo Very good effort, needs a little more work, but these tests will help a lot!

Comment on lines +736 to +741
echo \get_search_query( true ); // Ok.
echo \get_search_query( false ); // Bad.
echo MyNamespace\get_search_query( true ); // Bad.
echo \MyNamespace\get_search_query( true ); // Bad.
echo namespace\get_search_query( true ); // Bad. The sniff should stop flagging this once it can resolve relative namespaces.
echo namespace\Sub\get_search_query( true ); // Bad.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be tests here with FQN true/false and case-variations ? I.e. \TRUE and \False ?

Comment on lines 166 to 168
// PHPCS 3.13.3 changed the tokenization of FQN exit/die it impacts directly how this test case
// behaves (see https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1201).
664 => version_compare( $phpcs_version, '3.13.3', '>=' ) ? 1 : 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to remember we dropped support for PHPCS < 3.13.4 ? In which case, this toggle should no longer be needed.

<?php

echo PHP_VERSION_ID, PHP_VERSION, PHP_EOL, PHP_EXTRA_VERSION; // OK.
echo PHP_VERSION_ID, PHP_VERSION, \PHP_EOL, PHP_EXTRA_VERSION; // OK.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be tests with these "safe constants" in namespaced variants ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to also have some tests like the below ? (mind: I've not checked whether the function names in the second line (short echo) are the correct WP function names, this will need verification)

	?>
	<a href="<?php \the_permalink(); ?>" title="<?php \the_title_attribute(); ?>"><?php \the_title(); ?></a>
	<a href="<?= \get_permalink(); ?>" title="<?= \get_title_attribute(); ?>"><?= \get_the_title(); ?></a>
	<?php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And how about this ?

echo \true, \False, \NULL;

I know, it's kind of silly code, but the tokens are in the "safe tokens" list and with the fully qualified bit, the behaviour may change ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also see a separate category of array walking functions being handled. Do these need tests with the namespaced name variations too ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<a href="<?php \the_permalink(); ?>" title="<?php \the_title_attribute(); ?>"><?php \the_title(); ?></a>

@jrfnl, correct me if I'm missing something, but I'm not sure what the value of adding the test above would be. The sniff bails early when it encounters any of those three functions, as they are not in the list of functions that this sniff looks for. the_title_attribute() is an auto-escaping function, and the_permalink() and the_title() used to be (removed in #1547). But the code above does not trigger the check for auto-escaping functions, and there are already tests for this group of functions. Maybe the original test on line 9 needs to be removed or updated? Or am I missing its purpose?

Regarding the second line you suggested, I made a minor modification, and I believe it makes sense to add it to test multiple short open tags and fully qualified functions.

<a href="<?= \get_permalink(); ?>" title="<?= \the_title_attribute( array( 'echo' => false ) ); ?>"><?= \get_the_title(); ?></a><!-- Bad x 2. -->

@rodrigoprimo rodrigoprimo force-pushed the escape-output-namespaced-names-tests branch from 7190fe2 to d56798e Compare December 9, 2025 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants